package com.baseframe.sms.template;

import com.baseframe.sms.builds.SmsBuilds;
import com.baseframe.sms.entity.Sms;
import com.baseframe.sms.entity.SmsSendData;
import com.baseframe.sms.exception.SmsException;
import com.baseframe.sms.sign.SmsScene;
import com.baseframe.sms.sign.SmsType;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.data.EnvUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;


import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.extern.slf4j.Slf4j;

import java.util.*;


@Slf4j
public class TencentSmsTemplate extends AbstractSmsTemplate{
    public TencentSmsTemplate(Sms sms) {
        super(sms);
    }

    @Override
    public R<Object> send(SmsSendData sendData) {
        Credential cred = new Credential(getSms().getAccessKey(), getSms().getSecretKey());
        ClientProfile clientProfile = new ClientProfile();
        SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppId(getSms().getAppId());
        req.setSignName(getSms().getSign());
        req.setTemplateId(getSms().getTemplate());
        String phone =  "+86"+sendData.getPhone();
        String[] envStr = EnvUtil.getEnvStr(getSms().getTemplateContent());

        req.setPhoneNumberSet(phone.split(","));

        List<String> params = new ArrayList<>();

        //获取短信参数
        for (String env : envStr) {
            params.add(sendData.getParams().get(env));
        }

        req.setTemplateParamSet(params.toArray(new String[0]));


        try {
            SendSmsResponse res = client.SendSms(req);

            SendStatus[] sendStatusSet = res.getSendStatusSet();

            Set<String> codeSet = new HashSet<>();
            for (SendStatus sendStatus : sendStatusSet) {
                log.info("短信发送响应结果:{} {} {}",sendStatus.getPhoneNumber(),sendStatus.getCode(),sendStatus.getMessage());
                codeSet.add(sendStatus.getCode());
            }


            return codeSet.size()==1&&codeSet.contains("Ok")?R.success("短信发送成功"):R.fail("短信发送失败");
        } catch (TencentCloudSDKException e) {
            throw new SmsException(e);
        }

    }

    public static void main(String[] args) {


        Sms sms = new Sms();
        sms.setType(SmsType.TENCENT);
        sms.setScene(SmsScene.CAPTCHA);
        sms.setAccessKey("AKIDMZYCcolU1JOFrENHJ7h5E9pLiw9FD2jI");
        sms.setSecretKey("U6av571nQsNp8GDxeAUaR6240mRuAD6q");
        sms.setAppId("1400834257");
        sms.setSign("舞指科技");
        sms.setTemplate("1351858");
        sms.setTemplateContent("{1}，5分钟内有效，仅可用于账户身份验证。为了保障您的账户安全，请勿向他人泄露验证码信息。 ");

        SmsTemplate smsTemplate = SmsBuilds.getTemplate(sms);

        Map<String,String> params = new HashMap<>();
        params.put("1","123456");
        SmsSendData sendData = new SmsSendData();
        sendData.setPhone("17379635493");
        sendData.setParams(params);


        R<Object> send = smsTemplate.send(sendData);
        System.out.println(send);

    }
}
